package net.aib.moneytracker;

import android.content.Context;
import android.database.Cursor;
import net.aib.moneytracker.db.*;

public class MoneyTrackerController {
	
	private DBMoneyTracker dbMT;
	private DBHelper mDBHelper;
	
	public MoneyTrackerController(Context context){
		mDBHelper = new DBHelper(context);
		dbMT = new DBMoneyTracker(context);
		dbMT.open();
	}
	
	public void addToAccount(double amount){
		dbMT.addToAccount(amount);
	}
	
	public double getCurrentAccount(){
		double account = dbMT.getAccount();
		if(account == Double.MIN_VALUE)
		{
			account = 0;
		}
		return account; 
	}
	
	public void addNewGroup(String name){
		dbMT.createCategory(name);
	}
	
	public void addNewCompany(String name){
		dbMT.createCompany(name);
	}
	
	public Cursor getGroups(){
		return dbMT.fetchCategories();
	}
	
	public Cursor getCompanies(){
		return dbMT.fetchCompanies();
	}
	
	public Cursor getBuys(){
		return dbMT.fetchBuys();
	}
	
	public void newBuy(Double amount, long categoryid, long companyid){
		long buyid = dbMT.createBuy(amount, categoryid);
		dbMT.addBuyToCompany(buyid, companyid);
		dbMT.lowerAccount(amount);
	}
	
	public void deleteCategory(long categoryid){
		dbMT.deleteCategory(categoryid);
	}
	
	public void editCategory(long categoryid, String name){
		dbMT.updateCategory(categoryid, name);
	}
	
	public String getCategory(long categoryid){
		Cursor category = dbMT.fetchCategory(categoryid);
		category.moveToFirst();
		return category.getString(category.getColumnIndex(mDBHelper.CATEGORY_KEY_NAME));
	}
	
	public void deleteCompany(long companyid){
		dbMT.deleteCompany(companyid);
	}
	
	public void editCompany(long companyid, String name){
		dbMT.updateCompany(companyid, name);
	}
	
	public String getCompany(long companyid){
		Cursor company = dbMT.fetchCompany(companyid);
		company.moveToFirst();
		return company.getString(company.getColumnIndex(mDBHelper.COMPANY_KEY_NAME));
	}
	
	public Cursor getBuy(long buyid){
		Cursor buy = dbMT.fetchBuy(buyid);
		buy.moveToFirst();
		return buy;
	}
	
	public void editBuy(long id, Double amount)
	{
		dbMT.updateBuy(id, amount);
	}
}
